pub mod jwt;
pub mod router;

use axum::{
    routing::{get, post},
    Router,
};
use sqlx::sqlite::SqlitePoolOptions;
use std::net::SocketAddr;

use crate::router::{
    root::index_handler,
    todo::{todo_create_handler, todo_delete_handler, todo_update_handler, todos_handler, Db},
    user::login_handler,
};

#[tokio::main]
async fn main() {
    // initialize tracing
    tracing_subscriber::fmt::init();

    // sqlite pool
    let pool = SqlitePoolOptions::new()
        .max_connections(8)
        .connect("todo.db")
        .await
        .unwrap();

    // build our application with a route
    let app = Router::new()
        .route("/", get(index_handler))
        .route("/login", post(login_handler))
        .route(
            "/todos",
            get(todos_handler)
                .put(todo_update_handler)
                .post(todo_create_handler)
                .delete(todo_delete_handler)
                .with_state(Db::default()),
        )
        .with_state(pool);

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    tracing::debug!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}
